package cn.gson.springboot.model.service.saleservice;

import cn.gson.springboot.model.dao.cusdao.CustomerDao;
import cn.gson.springboot.model.dao.presaledao.BusinessInfoDao;
import cn.gson.springboot.model.dao.saledao.ContractDao;
import cn.gson.springboot.model.mapper.presalemapper.PsBusinessDetailsTwoMapper;
import cn.gson.springboot.model.mapper.salemapper.ContractMapper;
import cn.gson.springboot.model.pjos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ContractService {
    @Autowired
    ContractMapper contractMapper;
    @Autowired
    ContractDao contractDao;
    @Autowired
    PsBusinessDetailsTwoMapper businessDetailsTwoMapper;
    @Autowired
    BusinessInfoDao businessInfoDao;//商机dao
    @Autowired
    CustomerDao customerDao;




    /**
     * 合同查询
     * int empid
     * @return
     */
    public List<Contract> contractsInfo(Integer deptId,Integer empId,Integer type,String name){
        List<Contract> contracts = contractMapper.contractInfo(deptId,empId,type,name);
        return  contracts;
    }

    /**
     * 合同详情
     * @param contid
     * @return
     */
    public Contract contractDetails(Integer contid){
        Contract contract = contractMapper.contractDetails(contid);
        return  contract;
    }

    /**
     * 客户对应商机查询
     * @param customId
     * @return
     */
    public List<BusinessInfo> opportunityList(Integer customId){
        List<BusinessInfo> businessInfos = contractMapper.opportunityList(customId);
        return businessInfos;
    }

    /**
     * 客户报价单查询
     * @param customId
     * @return
     */
    public List<Offer> contOfferList(Integer customId,Integer busId){
        List<Offer> offers = contractMapper.contOfferDetails(customId,busId);
        return offers;
    }

    /**
     * 付款方式查询
     * @return
     */
    public List<TermsPayment> paymentList(){
        List<TermsPayment> termsPayments = contractMapper.paymentDetails();
        return termsPayments;
    }

    /**
     * 新增合同
     * @param contract
     * @return
     */
    public int  save(Contract contract){
        Contract save = contractDao.save(contract);
        if(save!=null){
            //推进商机进度
            SpeedInfo speedInfo = businessDetailsTwoMapper.selectOne("签单");
            BusinessInfo businessInfo = businessInfoDao.findById(contract.getBusinessInfoByBusinesId().getBusinesId()).get();
            businessInfo.setSpeedInfoBySpeedId(speedInfo);
            businessInfo.setOpportunityStatus(4);
            businessInfoDao.save(businessInfo);

            Integer integer = contractMapper.contrCustomType(contract.getCustomerByCustomId().getCustomId());
            CustomerType type= new CustomerType();
            if(integer<1){
                //签约客户
                type.setTypeId(3);
            }else{
                //重复购买
                type.setTypeId(2);
            }
            //推进客户未签约客户
            Customer customer = customerDao.findById(contract.getCustomerByCustomId().getCustomId()).get();
            customer.setTypeId(type);
            return 200;
        }else {
            return 500;
        }
    }

    /**
     * 新增订单查询合同
     * @param empId
     * @return
     */
    public List<Contract> orderContractInfo(Integer empId,String name){
        List<Contract> contracts = contractMapper.addOrderInfo(empId,name);
        return contracts;
    }

    /**
     * 合同名称
     */
    public String selectMaxContractName(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = contractMapper.selectMaxContractName();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "H"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "H"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    };

    /**
     * 订单编号
     * @return
     */
    public String selectMaxOrderName(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = contractMapper.selectMaxOrdertName();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "D"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "D"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    }

    /**
     * 发货单编号
     * @return
     */
    public String selectMaxZfhuodanName(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = contractMapper.  selectMaxZfhuodanName();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "D"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "F"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    }

}
